package com.xgggh.redpack.ui.home.viewbinder;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.xgggh.redpack.R;
import com.xgggh.redpack.base.viewbinder.BaseItemViewBinder;
import com.xgggh.redpack.base.viewbinder.BinderViewHolder;
import com.xgggh.redpack.databinding.ContentNavHomeBinding;
import com.xgggh.redpack.domain.home.NavHome;
import com.xgggh.redpack.domain.redpack.Pack;

import java.util.List;

import static com.baidu.mapapi.BMapManager.getContext;

public class NavHomeViewBinder extends BaseItemViewBinder<NavHome>  {
    RefreshListenerAdapter onRefreshListener;
    ContentNavHomeBinding binding;

    public NavHomeViewBinder(RefreshListenerAdapter onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }

    @NonNull
    @Override
    public int getResId() {
        return R.layout.content_nav_home;
    }

    @Override
    protected void onBindViewHolder(@NonNull BinderViewHolder holder, @NonNull NavHome item) {
        super.onBindViewHolder(holder, item);
        binding= DataBindingUtil.getBinding(holder.itemView);
        initRecyclerView(binding,item);
    }


    private void initRecyclerView(ContentNavHomeBinding binding,NavHome item){
        //下拉刷新
        binding.twinklingRefreshLayout.setOnRefreshListener(onRefreshListener);

        adapter.register(Pack.class, new HomeViewBinder());
        binding.recyclerView.setAdapter(adapter);

        DividerItemDecoration divider = new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(),R.drawable.home_divider));
        binding.recyclerView.addItemDecoration(divider);
        if (item.getPackList()!=null && item.getPackList().size()!=0){
            items.addAll(item.getPackList());
        }
        adapter.setItems(items);
        adapter.notifyDataSetChanged();

        /* 断言所有使用的类型都已注册 */
        //assertAllRegistered(adapter, items);
        /* 断言 recyclerView 使用的是正确的 adapter */
        //assertHasTheSameAdapter( mBinding.appBarHome.contentHome.recyclerView, adapter);
       // getRedPack(firstPage());
    }

    public void updateData(List<Pack> packList,boolean isFirst){
        if (isFirst){
            items.clear();
        }
        if (packList!=null && packList.size()>0){
            items.addAll(packList);
        }
        adapter.notifyDataSetChanged();
    }

    public void closeRefreshUI(){
        binding.twinklingRefreshLayout.finishRefreshing();
        binding.twinklingRefreshLayout.finishLoadmore();
    }


}
